package com.features;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import com.utils.AbstractTrace;

/** <p>Title: 硬盘资源信息</p>
 * <p>Description: 获取硬盘资源信息</p>
 * <p>Copyright: Copyright (c) 2011</p>
 * @author zhangzuoqiang
 * @version 
 * <br/>Date: 2011-5-10
 */
public class FileSysCommand extends AbstractTrace {
	
	/**
	 * 取硬盘已有的分区及其详细信息
	 * @throws Exception
	 */
	public void getFileSystemInfo() {
		Sigar sigar = new Sigar();
        FileSystem fslist[] = null;
		try {
			fslist = sigar.getFileSystemList();
		} catch (SigarException e1) {
			e1.printStackTrace();
		}
		
        for (int i = 0; i < fslist.length; i++) {
        	traceln("\n~~~~~~~~~~" + i + "~~~~~~~~~~");
            FileSystem fs = fslist[i];
            // 分区的盘符名称
            traceln("fs.getDevName() = " + fs.getDevName());
            // 分区的盘符名称
            traceln("fs.getDirName() = " + fs.getDirName());
            traceln("fs.getFlags() = " + fs.getFlags());//
            // 文件系统类型，比如 FAT32、NTFS
            traceln("fs.getSysTypeName() = " + fs.getSysTypeName());
            // 文件系统类型名，比如本地硬盘、光驱、网络文件系统等
            traceln("fs.getTypeName() = " + fs.getTypeName());
            // 文件系统类型
            System.out.println("fs.getType() = " + fs.getType());
            FileSystemUsage usage = null;
            try {
            	usage = sigar.getFileSystemUsage(fs.getDirName());
            } catch (SigarException e) {
            	if (fs.getType() == 2)
					try {
						throw e;
					} catch (SigarException e1) {
						e1.printStackTrace();
					}
            	continue;
            }
            
            switch (fs.getType()) {
            	case 0: // TYPE_UNKNOWN ：未知
            		break;
        		case 1: // TYPE_NONE
        			break;
    			case 2: // TYPE_LOCAL_DISK : 本地硬盘
    				// 文件系统总大小
    				traceln(" Total = " + usage.getTotal() + "KB");
    				// 文件系统剩余大小
    				traceln(" Free = " + usage.getFree() + "KB");
    				// 文件系统可用大小
    				traceln(" Avail = " + usage.getAvail() + "KB");
    				// 文件系统已经使用量
    				traceln(" Used = " + usage.getUsed() + "KB");
    				double usePercent = usage.getUsePercent() * 100D;
    				// 文件系统资源的利用率
    				traceln(" Usage = " + usePercent + "%");
    				break;
				case 3:// TYPE_NETWORK ：网络
					break;
				case 4:// TYPE_RAM_DISK ：闪存
					break;
				case 5:// TYPE_CDROM ：光驱
					break;
				case 6:// TYPE_SWAP ：页面交换
					break;
				}
				traceln(" DiskReads = " + usage.getDiskReads());
				traceln(" DiskWrites = " + usage.getDiskWrites());
			}
        return;
       }
}
